<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index extends Controller_Base {

	public function action_index()
	{
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
        
	}
    
    /**
     * 
     * @param type $random  保证前台ajax每次请求验证码不一样
     */
    public function action_captcha($random='')
    {
        @ob_end_clean(); //清除之前出现的多余输入
        $config=  Kohana::$config->load('config')->captcha;
        $img = new Captcha(RESOURCE . 'images/captcha/', $config['width'], $config['height']);
        $img->session_word = 'captcha_word';
        $img->generate_image();
        exit;
    }
    
    public function action_check()
    {
        $this->template->site_title='后台登录';
        $word= Arr::get($_POST,'captcha');
        $img=Captcha::factory();
        $img->session_word = 'captcha_word';
       if( $img->check_word($word))
       { 
           $userName=Arr::get($_POST,'username');
           $userPassword=Arr::get($_POST,'password');
           $where=array('user_name'=>$userName);
           $salt=DB::select('ko_salt')
                   ->from('admin_user')
                   ->where('user_name','=',$userName)
                   ->execute()
                   ->get('ko_salt');
          $dbPassword=!empty($salt)?md5(md5($userPassword).$salt):md5($userPassword);
          /* 检查密码是否正确 */
          $row=DB::select('user_id','user_name','password', 'last_login', 'action_list', 'last_login','ko_salt')
                  ->from('admin_user')
                  ->where('user_name','=',$userName)
                  ->where('password','=',$dbPassword)
                  ->execute()
                  ->as_array();
          
          if($row)
          {
               $row=$row[0];
               $this->_set_admin_session($row);
               // 更新最后登录时间和IP
               $update=array('last_login'=>time(),'last_ip'=>common::getClientIp());
               DB::update('admin_user')
                       ->set($update)
                       ->where('user_id','=', $row['user_id'])
                       ->execute();
               
                if (isset($_POST['remember']))
                {
                    $time = time() + 3600 * 24 * 365;
                    $this->sessionConfig = Kohana::$config->load('session');
                    Cookie::$salt =$this->sessionConfig['cookie']['salt'];
                    Cookie::set('user_id',   $row['user_id'],$time);
                    Cookie::set('password', md5($row['password'] . $this->config->hashCode), $time);
                }
                $this->request->redirect('http://admin.ko.com');
               
          }  else {
              $this->show_message('错误用户信息',-1);
          }
          
       }else{
           $this->show_message('验证码错误');
       }
       die;
    }
    
    private function _set_admin_session($data)
    {
        foreach ($data as $key => $value)
        {
             Session::instance('native')->set($key, $value);
        }
    }

} // End Welcome
